package com.vnp.vchat.UIControls;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class MyCircleImageView extends ImageView {

	Context mContext;
	int R = 100;

	public MyCircleImageView(Context context) {
		super(context);
		mContext = context;
	}

	public MyCircleImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	OnImageLoadedListiner onImageLoadedListiner;

	public void setOnImageLoadedListiner(
			OnImageLoadedListiner onImageLoadedListiner) {
		this.onImageLoadedListiner = onImageLoadedListiner;
	}

	public void setR(int r) {
		R = r;
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		if (null == bm)
			return;
		bm = cropToSquare(bm);
		Bitmap resized = Bitmap.createScaledBitmap(bm, R, R, true);
		Bitmap circle = getRoundedCornerBitmap(resized, R);
		super.setImageBitmap(circle);
		if (onImageLoadedListiner != null)
			onImageLoadedListiner.onLoaded();
	}

	@Override
	public void setBackgroundResource(int resid) {
		super.setBackgroundResource(resid);
		if (onImageLoadedListiner != null)
			onImageLoadedListiner.onLoaded();
	}

	@Override
	public void setBackgroundDrawable(Drawable background) {
		super.setBackgroundDrawable(background);
		if (onImageLoadedListiner != null)
			onImageLoadedListiner.onLoaded();
	}

	public static interface OnImageLoadedListiner {
		public void onLoaded();
	}

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}

	public static Bitmap drawableToBitmap(Drawable drawable) {
		if (drawable instanceof BitmapDrawable) {
			return ((BitmapDrawable) drawable).getBitmap();
		}

		Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
		drawable.draw(canvas);

		return bitmap;
	}

	public Bitmap cropToSquare(Bitmap srcBmp) {
		Bitmap dstBmp = null;
		if (srcBmp.getWidth() >= srcBmp.getHeight()) {

			dstBmp = Bitmap.createBitmap(srcBmp,
					srcBmp.getWidth() / 2 - srcBmp.getHeight() / 2, 0,
					srcBmp.getHeight(), srcBmp.getHeight());

		} else {
			try {
				dstBmp = Bitmap.createBitmap(srcBmp, 0, srcBmp.getHeight() / 2
						- srcBmp.getWidth() / 2, srcBmp.getWidth(),
						srcBmp.getWidth());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dstBmp;
	}

}
